#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>

using namespace std;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *run(void *args)
{
    string name = static_cast<const char *>(args);
    while (true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        cout << "新线程：" << name << endl;
        pthread_mutex_unlock(&mutex);
    }

    return nullptr;
}

int main()
{
    pthread_t t1, t2;
    pthread_create(&t1, nullptr, run, (void *)"thread-1");
    pthread_create(&t1, nullptr, run, (void *)"thread-2");

    cout << "主线程开始控制" << endl;

    while (true)
    {
        pthread_cond_broadcast(&cond);
        sleep(1);
    }
    return 0;
}